package com.wpi.iCoach;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

/**
 * This an an ArrayAdapter used to create a graphical list of locations
 * to be displayed to the user based upon an array of LocationEntry. This
 * class should be used in conjunction with the Android ListAdapter view. 
 * 
 * @author Kenneth J. Loomis
 * @version June 2012
 */
public class LocationAdapter extends ArrayAdapter<LocationEntry> {

	/** The activity context that uses this adapter. */
    private Context context;
    
    /** The id of the layout. */
    private int layoutResourceId;   
    
    /** The array of Location Entries used by the adapter */
    private LocationEntry data[] = null;
   
    /**
     * The constructor. Stores the parameters as class members.
     * @param context (The activity context that uses this adapter.)
     * @param layoutResourceId (The id of the layout.)
     * @param data (The array of LocationEntry used by the adapter.)
     */
    public LocationAdapter(Context context, int layoutResourceId, LocationEntry[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    /**
     * Gets the view for the position provided. Meaning it converts a
     * LocationEntry in the array to a graphical representation. First the
     * graphical representation of the entry (location_view.xml) in the list 
     * is bound to a holder class. Then, the values holder class are modified
     * to match the values in the LocationEntry object. 
     * (This is not called directly; called by the ListAdapter view to
     * generate the graphical representation of this list.) 
     * @param position (Position in the LocationEntry array.)
     * @param convertView (The view that will be converted.) 
     * @param parent
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LocationHolder holder = null;
       
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
           
            holder = new LocationHolder();
            holder.tvName = (TextView)row.findViewById(R.id.tvName);
            holder.tvDistance = (TextView)row.findViewById(R.id.tvDistance);
            holder.tvLat = (TextView)row.findViewById(R.id.tvLat);
            holder.tvLon = (TextView)row.findViewById(R.id.tvLon);
            holder.tvVisit = (TextView)row.findViewById(R.id.tvVisits);
            holder.rbLikeness = (RatingBar)row.findViewById(R.id.rbLikeness);
           
            row.setTag(holder);
        }
        else
        {
            holder = (LocationHolder)row.getTag();
        }
       
        LocationEntry le = data[position];
        holder.tvName.setText(le.locName);
        holder.tvDistance.setText(le.dist);
        holder.tvLat.setText(Double.toString(le.lat));
        holder.tvLon.setText(Double.toString(le.lon));
        holder.tvVisit.setText("# Visits: " + Integer.toString(le.visit));
        holder.rbLikeness.setRating(le.likeness);
       
        return row;
    }
   
    /**
     * This class is used by the LocationAdapter class to provide a view holder
     * for the views that make up an item in the graphical list of locations
     * deplayed to the user.
     * 
     * @author Kenneth J. Loomis (kjloomis)
     * @version June 2012
     */
    static class LocationHolder
    {
    	/** Textview for displaying the name of the location. */
        TextView tvName;
        
        /** Textview for displaying the distance of the location from the user's current location. */
        TextView tvDistance;
        
        /** Textview for displaying the latitude of the location */
        TextView tvLat; // This view is currently non-visible in the displayed list.
        
        /** Textview for displaying the longitude of the location */
        TextView tvLon; // This view is currently non-visible in the displayed list.
        
        /** Textview for displaying the number of visits recorded for the location. */
        TextView tvVisit;
        
        /** Star rating bar used for displaying the rating of the location. */
        RatingBar rbLikeness;
    }
	
}
